package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.major;
import pojo.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class bindController {

    @RequestMapping("/showBind")
    public String showName(HttpServletRequest request, HttpSession session, Model model, ModelMap modelMap)
    {
        System.out.println(request);
        System.out.println(session);
        model.addAttribute("msg","getName");
        System.out.println(model);
        modelMap.addAttribute("msg",1);
        System.out.println(modelMap);
        //访问传递的参数
        String name = request.getParameter("name");
        System.out.println("查询到的name："+name);
        return "index";
    }
    @RequestMapping("/showParam")
    public String showParam(@RequestParam("username") String name)
    {
        //访问传递的参数
        System.out.println("查询到的name："+name);
        return "index";
    }
    @RequestMapping("/showPathVariable/{name}")
    public String showPathVariable(@PathVariable("name") String name)
    {
        //访问传递的参数
        System.out.println("查询到的name："+name);
        return "index";
    }
    @RequestMapping("/register")
    public String register(user user)
    {
        //访问传递的参数
        System.out.println("查询到的user："+user);
        return "index";
    }

    @RequestMapping("/getUserMajor")
    public String getUserMajor(user user)
    {
        //访问传递的参数
        System.out.println("user的信息："+user.getName());
        System.out.println("user的major："+user.getMajor().getName());
        return "index";
    }
//    @RequestMapping("/showMajor")
//    public String showMajor(user user)
//    {
//        List<major> majorList = user.getMajors();
//        //访问传递的参数
//        for (int i = 0; i < majorList.size(); i++) {
//            major major = majorList.get(i);
//            System.out.println("major："+major.getName()+"id："+major.getId());
//        }
//        return "index";
//    }
    @RequestMapping("/getDay")
    public String getDay(Date date)
    {

        //访问传递的参数
        System.out.println("date："+date);
        return "index";
    }
    @RequestMapping("/bindArray")
    public String bindArray(String[] names)
    {

        //访问传递的参数
        for (String name : names) {
            System.out.println("majorName："+name);
        }
        return "index";
    }
    @RequestMapping("/bindList")
    public String bindList(@RequestParam("names") List<String> names)
    {

        //访问传递的参数
        for (String name : names) {
            System.out.println("majorName："+name);
        }
        return "index";
    }
    @RequestMapping("/bindMajor")
    public String bindmajor(@RequestBody major major)
    {

        //访问传递的参数
        System.out.println("单个major："+major);
        return "index";
    }
    @RequestMapping("/bindMajors")
    public String bindmajors(@RequestBody List<major> majorList)
    {

        //访问传递的参数
        System.out.println("多个major：");
        majorList.forEach(major ->
                System.out.println(major));
        return "index";
    }
}
